package cn.it58.gooditname.settings

import cn.it58.gooditname.services.ConfigService
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.options.ConfigurationException
import javax.swing.JComponent

/**
 * GoodName 插件配置页面
 */
class GoodItNameConfigurable : Configurable {

    private var settingsPanel: GoodItNameSettingsPanel? = null

    override fun getDisplayName(): String {
        return "GoodName"
    }

    override fun createComponent(): JComponent? {
        if (settingsPanel == null) {
            settingsPanel = GoodItNameSettingsPanel()
        }
        return settingsPanel?.mainPanel
    }

    override fun isModified(): Boolean {
        val panel = settingsPanel ?: return false
        val configService = ConfigService.getInstance()

        return panel.apiKey != configService.getApiKey() ||
                panel.aiModel != configService.getAiModel() ||
                panel.isApiEnabled != configService.isApiEnabled() ||
                panel.isFallbackLocalEnabled != configService.isFallbackLocalEnabled()
    }

    @Throws(ConfigurationException::class)
    override fun apply() {
        val panel = settingsPanel ?: return
        val configService = ConfigService.getInstance()

        configService.setApiKey(panel.apiKey)
        configService.setAiModel(panel.aiModel)
        configService.setApiEnabled(panel.isApiEnabled)
        configService.setFallbackLocalEnabled(panel.isFallbackLocalEnabled)
    }

    override fun reset() {
        val panel = settingsPanel ?: return
        val configService = ConfigService.getInstance()

        panel.apiKey = configService.getApiKey()
        panel.aiModel = configService.getAiModel()
        panel.isApiEnabled = configService.isApiEnabled()
        panel.isFallbackLocalEnabled = configService.isFallbackLocalEnabled()
    }

    override fun disposeUIResources() {
        settingsPanel = null
    }
} 